//
//  LXSignInViewController.m
//  跑跑快销
//
//  Created by linxian on 14-5-8.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "LXSignInViewController.h"

@interface LXSignInViewController ()

@end

@implementation LXSignInViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"签到";
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    _mapView.delegate = nil;
    [_mapView setShowsUserLocation:NO];
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    
    [self initArray];
    [self addBarButtons];
    
    _signInVi = [[LXSignInView alloc] init];
    self.view = _signInVi;
    
    
    _isUpdate = YES;
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectZero];
    [_mapView setUserTrackingMode:BMKUserTrackingModeNone];
    [_mapView setShowsUserLocation:YES];
    [self.view addSubview:_mapView];
    [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"正在定位中，请稍后。。。"];
}

- (void)addBarButtons
{
    // 左侧按钮
    UIButton *blankBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    blankBtn.backgroundColor = [UIColor clearColor];
    [blankBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem * blankButton = [[UIBarButtonItem alloc] initWithCustomView:blankBtn];

    [self.navigationItem setLeftBarButtonItem:blankButton];
   
    // “退出”按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [leftButton setTitle:@"退出" forState:UIControlStateNormal];
    [leftButton setTitle:@" " forState:UIControlStateHighlighted];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighlighted];
    leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [leftButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    leftButton.tag = 1;
    UIBarButtonItem * fliterButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    
    // “下一步”按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [rightButton setTitle:@"继续" forState:UIControlStateNormal];
    [rightButton setTitle:@" " forState:UIControlStateHighlighted];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
    //    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighligh ted];
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [rightButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    rightButton.tag = 2;
    UIBarButtonItem * mapButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    NSArray *btns = [NSArray arrayWithObjects:mapButton, fliterButton, nil];
    [self.navigationItem setRightBarButtonItems:btns];
    
}

- (void)down:(UIBarButtonItem *)sender
{
    if (sender.tag == 1) {
        // 退出
        NSLog(@"退出");
        UIViewController * rootVC = [self.navigationController.viewControllers objectAtIndex:1];
        [self.navigationController popToViewController:rootVC animated:YES];
    } else {
        // 继续
        NSLog(@"继续");
        LXOrderViewController *oVC = [[LXOrderViewController alloc] init];
        [self.navigationController pushViewController:oVC animated:YES];
    }
}


- (void)initArray
{
    NSArray *iArray = [NSArray arrayWithObjects:@"pass.png",@"pass.png",nil];
    _iArray = iArray;
    NSArray *industryArray = [NSArray arrayWithObjects:@"电话签到",@"修复位置",nil];
    _industryArray = industryArray;
    
    _selectRow = 0;
}



#pragma mark - BMKMapViewDelegate
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
    if (_isUpdate) {
        _isUpdate = NO;
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"拜访成功。"];
        [_mapView setShowsUserLocation:NO];
    }
}

- (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
    //[[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"定位失败！！"];
    
    // 弹出签到失败的处理框
    LXCustomActionSheet *lxCAS = [[LXCustomActionSheet alloc] initWithTitle:@"距离太远  无法签到" withIconArray:_iArray withMsgArray:_industryArray withBtnTitle:nil withSelectArray:nil];
    
    lxCAS.delegate = self;
    [lxCAS showInView:_signInVi];
}

#pragma mark - actionsheet的代理
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    // 按钮后处理在这
    LXCustomActionSheet *asheet = (LXCustomActionSheet *)actionSheet;
    if (buttonIndex >= 1 && buttonIndex <= asheet.messageArray.count) {
        NSString *str = [asheet.messageArray objectAtIndex:buttonIndex-1];
        NSLog(@"%@",str);
        _selectRow = buttonIndex-1;
        
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
